package com.zy.Conterller;

import com.zy.pojo.Delivered;
import com.zy.pojo.Goods;
import com.zy.vo.kucun.GoodsVo;
import com.zy.vo.ResultVo;
import com.zy.service.GoodsService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;

@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Resource
    private GoodsService goodsService;
    @GetMapping("/pagelist/{pageNo}/{pageSize}")
    public ResultVo getGoodsPageList(@PathVariable Integer pageNo, @PathVariable Integer pageSize, GoodsVo goodsVo){
        return ResultVo.success(goodsService.getPageGoodsList(pageNo,pageSize,goodsVo));
    }
    @GetMapping("list")
    public ResultVo getGoodeList(){
        return ResultVo.success(goodsService.getGoodeselect());
    }
    @GetMapping("/goodsdel/{id}")
    public  ResultVo del(@PathVariable String id){
        try {
            goodsService.getGoodsDelete(Integer.parseInt(id));
            return ResultVo.success("角色删除成功!!!");
        }catch (Exception e){

            return ResultVo.fail("角色删除失败!!!",e);
        }
    }
    @PostMapping("/goodsaddCk/{Goodsquantity}")
    public ResultVo getHousedAdd(@RequestBody Goods goods, Delivered delivered, @PathVariable Integer Goodsquantity, HttpSession session ){

        if (goods.getQuantity()==0){
            return ResultVo.fail("该商品暂时没有库存出库失败!");
        }else if (Goodsquantity==0){
            return ResultVo.fail("商品出库数量不能为0!");
        }else if (Goodsquantity>goods.getQuantity()){
            return ResultVo.fail("该商品暂时库存不充足出库失败!");
        }
        try{
            if (goods.getQuantity()!=0&&goods.getQuantity()!=null){
                goods.setGoodsData(new Date());
                goods.setQuantity(Goodsquantity);
                goodsService.getGoodsDeAdd(goods);
                return ResultVo.success("出库商品成功!!!");
            }

        }catch (Exception e){
            return ResultVo.fail("出库商品失败",e);//打印错误日志
        }
        return null;
    }

}
